package com.haojiang.mh

import android.Manifest
import android.os.Build
import android.util.Log
import android.view.View
import com.haojiang.mh.ui.activity.ChooseWebServiceActivity
import com.haojiang.mh.ui.activity.MainActivity
import com.haojiang.mh.ui.activity.PermissionActivity
import com.haojiang.mh.ui.base.BaseActivity
import com.yanzhenjie.permission.AndPermission
import kotlinx.coroutines.*
import java.util.logging.Logger

/**
 * 启动页
 *
 * Created by ZapFive on 2019-05-25.
 *
 * wuzhuang@mirahome.me
 */
class StartActivity : BaseActivity() {

    private var permissions = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_FINE_LOCATION
    )

    override fun beforeSetContentView() {
        isNeedSetStatusBar = false
        isNeedWiFiOrBluetoothTip = false

        var flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        flag = flag or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        flag = flag or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        flag = flag or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            flag = flag or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        }
        window.decorView.systemUiVisibility = flag
    }

    override fun getLayoutId() = R.layout.activity_start

    override fun getTitleName(): String? = null

    override fun initActivity() {
        if (!isTaskRoot) {//防止重重复启动Launcher
            isInterruptProcess = true
            context.finish()
            return
        }
    }

    override fun initView() {
        GlobalScope.launch {
            delay(1_000L)
            withContext(Dispatchers.Main) {
                redirectNoParamAndFinish(MainActivity::class.java)
//                if (AndPermission.hasPermissions(context, *permissions)) {
//                    Log.e("colin", "已经授权")
//                    if (isFirstLauncher()) {
//                        redirectNoParamAndFinish(ChooseWebServiceActivity::class.java)
//                    } else {
//                        redirectNoParamAndFinish(MainActivity::class.java)
//                    }
//                } else {
//                    Log.e("colin", "没有授权")
//                    redirectNoParamAndFinish(PermissionActivity::class.java)
//                }
            }
        }
    }
}